﻿$(document).ready(function (){
	var question = 0;
	var correct = 0;
	//test
	var robotId = 15;
	
	$.ajax({

		type:"GET",
		url:"index/quiz/",
		async:true,
		beforeSend:function loading(){
		},
		success:function(v){
			$("#map_school").append("<p>2000</p>");
			var data = window.JSON.parse(v);
			var myInterval = window.setInterval(function(){
				var q = data[question].question;
				var a = data[question].answer;
				$("#quizAsk").append("Question" +": "+q+"\n");
				while(q.indexOf("?") >= 0 || q.indexOf("#") >= 0 || q.indexOf("%") >= 0){
					q = q.replace("?","");
					q = q.replace("#","");
					q = q.replace("%","");
				}	
				$.ajax({
					type:"GET",
					url:"index/quiztest/question/"+q+"/id/"+robotId,
					async:true,
					beforeSend:function loading(){
					},
					success:function(b){
						$("#quizAnswer").append("Answer" +": "+b+"\n");
						if(b == a){
							$("#quizAnswer").append("Correct!!!\n");
							$("#quizStar"+correct).attr("src","images/star.png");
							correct ++;
						}else{
							$("#quizAnswer").append("Wrong!!!\n");
						}
					},
					error:function(){
					}
				});	
				question++;
				if(question == 5){
					window.clearInterval(myInterval);
					if(correct==5){
						alert("You have passed the exam");
						$("#map_school").hide(2000);
						$("#map-mainAlanStreet").show(2000);
					}else{
						alert("You failed the exam");
						$("#map_school").hide(2000);
						$("#map-mainAlanStreet").show(2000);
					}
				}
			},5000);
		},
		error:function(){
		}
	});				
});